package org.iabako.server.dao.business;

import org.iabako.server.dao.JpaDAO;
import org.iabako.server.tools.BundleMessages;
import org.iabako.shared.entity.business.Sale;
import org.iabako.shared.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by jose on 14/07/15.
 */

@Repository("quoteDAO")
public class QuoteDAO extends JpaDAO<Long, Sale> {

    @Autowired
    BundleMessages messages;

    @Override
    protected String getUniqueNumberVerificationQuery() {
        return "SELECT s.id FROM Sale s WHERE s.number = :number AND s.enterprise IN (:enterprises) AND s.quote = 1";
    }

    @Override
    protected String getLastNumberQuery() {
        return "SELECT MAX(s.number) FROM Sale s WHERE s.enterprise IN (:enterprises) AND s.quote = 1";
    }

    @Override
    public String getNextAvailableId() {
        User user = getSessionUser();
        String nextId = super.getNextAvailableId();
        if (nextId.equals("000001")) {
            //Initialize first number prefix
            nextId = messages.getLabel(user.getEnterprise().getLanguage(), "quote_file_name") + nextId;
        }
        return nextId;
    }
}
